package com.dsjy.yf.address.controller;

import com.dsjy.yf.address.annotation.FocusAnnotation;
import com.dsjy.yf.address.component.MyApplicationRunner;
import com.dsjy.yf.address.config.base.BizException;
import com.dsjy.yf.address.config.base.CommonEnum;
import com.dsjy.yf.address.entity.dataobject.*;
import com.dsjy.yf.address.entity.myenum.OperateEnum;
import com.dsjy.yf.address.entity.myenum.TableEnum;
import com.dsjy.yf.address.entity.query.PageSearchQuery;
import com.dsjy.yf.address.entity.query.SearchQuery;
import com.dsjy.yf.address.service.*;
import com.dsjy.yf.address.utils.IntegerConvertUtil;
import com.dsjy.yf.address.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author whj
 * @version 1.0
 * @date 2021-01-19 9:42
 */
@RestController
@FocusAnnotation
@RequestMapping("/backstage/")
@PreAuthorize("hasRole('USER') or hasRole('ADMIN')")
@CrossOrigin(origins = "*", originPatterns = "true")
public class BackstageController {
    @Autowired
    private UserUtil userUtil;

    @Autowired
    private MyApplicationRunner applicationRunner;
    @Autowired
    private CountyService countyService;
    @Autowired
    private PoiService poiService;
    @Autowired
    private ExclusivezoneService exclusivezoneService;
    @Autowired
    private TownService townService;
    @Autowired
    private CommunityService communityService;
    @Autowired
    private VillageService villageService;
    @Autowired
    private BuildingService buildingService;
    @Autowired
    private StreetService streetService;
    @Autowired
    private YardService yardService;
    @Autowired
    private CommonService commonService;
    @Autowired
    private SubareaService subareaService;
    @Autowired
    private YardRoadService yardRoadService;

    @GetMapping("/getCounty")
    public List<CountyDO> getCountyDOList() {
        return countyService.searchAll();
    }

    @GetMapping("/getData")
    public Object getCommunityDOList(String code, String tableName) {
        Integer codeInt = IntegerConvertUtil.stringToInt(code);
        return commonService.searchWithCode(codeInt, tableName);
    }

    @GetMapping("/Test")
    public Object test() {
        return userUtil.isAdmin();
    }

    @PostMapping("/County/{operate}")
    public CountyDO addUpdDelCounty(@PathVariable String operate, @RequestBody CountyDO countyDO) {
        if (operate != null && countyDO != null) {
            Integer result = null;
            if (OperateEnum.DELETE.getOperate().equals(operate)) {
                result = commonService.deleteById(TableEnum.COUNTY.getTableName(), countyDO.getId());
            } else {
                result = countyService.addUpdDel(operate, countyDO);
            }
            if (result != null) {
                applicationRunner.initCounty();
                return countyDO;
            } else {
                throw new BizException(CommonEnum.PARAM_ERROR);
            }
        } else {
            throw new BizException(CommonEnum.PARAM_ERROR);
        }
    }

    @PostMapping("/Poi/{operate}")
    public PoiDO addUpdDelPoi(@PathVariable String operate, @RequestBody PoiDO poiDO) {
        if (operate != null && poiDO != null) {
            Integer result = null;
            if (OperateEnum.DELETE.getOperate().equals(operate)) {
                result = commonService.deleteById(TableEnum.POI.getTableName(), poiDO.getId());
            } else {
                result = poiService.addUpdDel(operate, poiDO);
            }
            if (result == null) {
                throw new BizException(CommonEnum.PARAM_ERROR);
            }
            return poiDO;
        } else {
            throw new BizException(CommonEnum.PARAM_ERROR);
        }
    }

    @PostMapping("/Exclusivezone/{operate}")
    public ExclusivezoneDO addUpdDelExclusivezone(@PathVariable String operate, @RequestBody ExclusivezoneDO exclusivezoneDO) {
        if (operate != null && exclusivezoneDO != null) {
            Integer result = null;
            if (OperateEnum.DELETE.getOperate().equals(operate)) {
                result = commonService.deleteById(TableEnum.EXCLUSIVEZONE.getTableName(), exclusivezoneDO.getId());
            } else {
                result = exclusivezoneService.addUpdDel(operate, exclusivezoneDO);
            }
            if (result == null) {
                throw new BizException(CommonEnum.PARAM_ERROR);
            }
            return exclusivezoneDO;
        } else {
            throw new BizException(CommonEnum.PARAM_ERROR);
        }
    }

    @PostMapping("/Town/{operate}")
    public TownDO addUpdDelTown(@PathVariable String operate, @RequestBody TownDO townDO) {
        if (operate != null && townDO != null) {
            Integer result = null;
            if (OperateEnum.DELETE.getOperate().equals(operate)) {
                result = commonService.deleteById(TableEnum.TOWN.getTableName(), townDO.getId());
            } else {
                result = townService.addUpdDel(operate, townDO);
            }
            if (result != null) {
                applicationRunner.initTown();
                return townDO;
            } else {
                throw new BizException(CommonEnum.PARAM_ERROR);
            }
        } else {
            throw new BizException(CommonEnum.PARAM_ERROR);
        }
    }

    @PostMapping("/Community/{operate}")
    public CommunityDO addUpdDelCommunity(@PathVariable String operate, @RequestBody CommunityDO communityDO) {
        if (operate != null && communityDO != null) {
            Integer result = null;
            if (OperateEnum.DELETE.getOperate().equals(operate)) {
                result = commonService.deleteById(TableEnum.COMMUNITY.getTableName(), communityDO.getId());
            } else {
                result = communityService.addUpdDel(operate, communityDO);
            }
            if (result != null) {
                applicationRunner.initCommunityListMap();
                return communityDO;
            } else {
                throw new BizException(CommonEnum.PARAM_ERROR);
            }
        } else {
            throw new BizException(CommonEnum.PARAM_ERROR);
        }
    }

    @PostMapping("/Village/{operate}")
    public VillageDO addUpdDelVillage(@PathVariable String operate, @RequestBody VillageDO villageDO) {
        if (operate != null && villageDO != null) {
            Integer result = null;
            if (OperateEnum.DELETE.getOperate().equals(operate)) {
                result = commonService.deleteById(TableEnum.VILLAGE.getTableName(), villageDO.getId());
            } else {
                result = villageService.addUpdDel(operate, villageDO);
            }
            if (result == null) {
                throw new BizException(CommonEnum.PARAM_ERROR);
            }
            return villageDO;
        } else {
            throw new BizException(CommonEnum.PARAM_ERROR);
        }
    }

    @PostMapping("/Street/{operate}")
    public StreetDO addUpdDelStreet(@PathVariable String operate, @RequestBody StreetDO streetDO) {
        if (operate != null && streetDO != null) {
            Integer result = null;
            if (OperateEnum.DELETE.getOperate().equals(operate)) {
                result = commonService.deleteById(TableEnum.STREET.getTableName(), streetDO.getId());
            } else {
                result = streetService.addUpdDel(operate, streetDO);
            }
            if (result == null) {
                throw new BizException(CommonEnum.PARAM_ERROR);
            }
            return streetDO;
        } else {
            throw new BizException(CommonEnum.PARAM_ERROR);
        }
    }

    @PostMapping("/Building/{operate}")
    public BuildingDO addUpdDelBuilding(@PathVariable String operate, @RequestBody BuildingDO buildingDO) {
        if (operate != null && buildingDO != null) {
            Integer result = null;
            if (OperateEnum.DELETE.getOperate().equals(operate)) {
                result = commonService.deleteById(TableEnum.BUILDING.getTableName(), buildingDO.getId());
            } else {
                result = buildingService.addUpdDel(operate, buildingDO);
            }
            if (result == null) {
                throw new BizException(CommonEnum.PARAM_ERROR);
            }
            return buildingDO;
        } else {
            throw new BizException(CommonEnum.PARAM_ERROR);
        }
    }

    @PostMapping("/Yard/{operate}")
    public YardDO addUpdDelYard(@PathVariable String operate, @RequestBody YardDO yardDO) {
        if (operate != null && yardDO != null) {
            Integer result = null;
            if (OperateEnum.DELETE.getOperate().equals(operate)) {
                result = commonService.deleteById(TableEnum.YARD.getTableName(), yardDO.getId());
            } else {
                result = yardService.addUpdDel(operate, yardDO);
            }
            if (result == null) {
                throw new BizException(CommonEnum.PARAM_ERROR);
            }
            return yardDO;
        } else {
            throw new BizException(CommonEnum.PARAM_ERROR);
        }
    }

    @PostMapping("/Subarea/{operate}")
    public SubareaDO addUpdDelSubarea(@PathVariable String operate, @RequestBody SubareaDO subareaDO) {
        if (operate != null && subareaDO != null) {
            Integer result = subareaService.addUpdDel(operate, subareaDO);
            if (result == null) {
                throw new BizException(CommonEnum.PARAM_ERROR);
            }
            return subareaDO;
        } else {
            throw new BizException(CommonEnum.PARAM_ERROR);
        }
    }

    @PostMapping("/YardRoad/{operate}")
    public YardRoadDO addUpdDelSubarea(@PathVariable String operate, @RequestBody YardRoadDO yardRoadDO) {
        if (operate != null && yardRoadDO != null) {
            Integer result = yardRoadService.addUpdDel(operate, yardRoadDO);
            if (result == null) {
                throw new BizException(CommonEnum.PARAM_ERROR);
            }
            return yardRoadDO;
        } else {
            throw new BizException(CommonEnum.PARAM_ERROR);
        }
    }

    @PostMapping("/search/page")
    public Object searchPageWithUser(@RequestBody PageSearchQuery pageSearchQuery) {
        if (pageSearchQuery.isNotNull()) {
            return commonService.searchWithPage(pageSearchQuery, false);
        } else {
            throw new BizException(CommonEnum.PARAM_ERROR);
        }
    }

    @PostMapping("/search/query")
    public Object searchWithQuery(@RequestBody SearchQuery searchQuery) {
        if (searchQuery.getTableName() != null) {
            return commonService.searchWithQuery(searchQuery, false);
        } else {
            throw new BizException(CommonEnum.PARAM_ERROR);
        }
    }
}
